from django.shortcuts import render
from text_analysis.text_segmentation import TextSegmentation
from text_analysis.text_similarity import TextSimilarity
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm


def text_segmentation(request):
    context = {}
    algorithm = TextSegmentation()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "text_analysis/1.html", context)


def text_similarity(request):
    context = {}
    algorithm = TextSimilarity()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "text_analysis/1.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "中文分词":
        algorithm.text = request.POST.get("text").strip()
        algorithm.custom_words = []
        for line in request.POST.get("custom_words").split("\n"):
            line = line.strip()
            if line != "":
                algorithm.custom_words.append(line)
                
    if algorithm.algorithm_name == "文本相似度":
        algorithm.text_1 = request.POST.get("text_1").strip()
        algorithm.text_2 = request.POST.get("text_2").strip()
        algorithm.text_3 = request.POST.get("text_3").strip()
    
    algorithm.implent()
    algorithm.generateNotebookFile()
    context["algorithm"] = algorithm
    return render(request, "text_analysis/5.html", context)
